from django.utils import timezone

from rest_framework import serializers
from ..models.sales import *


# 销售订单
class FaKuyouGoodsSalesOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = FaKuyouGoodsSalesOrder
        fields = '__all__'
        extra_kwargs = {
            'purchase_code': {'required': True},
            'number': {'min_value': 0},
            'all_money': {'min_value': 0}
        }

    def validate(self, data):
        """自定义验证逻辑"""
        if data.get('purchase_type') == '2' and not data.get('returns_order_id'):
            raise serializers.ValidationError("退货单必须关联原订单号")
        return data


# 销售订单详情
class FaKuyouGoodsSalesOrderInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = FaKuyouGoodsSalesOrderInfo
        fields = '__all__'
        extra_kwargs = {
            'goods_code': {'required': True},  # 确保必填字段
            'number': {'min_value': 0},
            'unit_price': {'min_value': 0}
        }


# 销售单
class FaKuyouGoodsSalesSerializer(serializers.ModelSerializer):
    order_type_display = serializers.CharField(
        source='get_order_type_display',
        read_only=True,
        help_text='单据类型显示值'
    )
    purchase_type_display = serializers.CharField(
        source='get_purchase_type_display',
        read_only=True,
        help_text='类型显示值'
    )
    pay_status_display = serializers.CharField(
        source='get_pay_status_display',
        read_only=True,
        help_text='收款状态显示值'
    )
    check_status_display = serializers.CharField(
        source='get_check_status_display',
        read_only=True,
        help_text='对单状态显示值'
    )
    examine_status_display = serializers.CharField(
        source='get_examine_status_display',
        read_only=True,
        help_text='审核状态显示值'
    )

    class Meta:
        model = FaKuyouGoodsSales
        fields = '__all__'

    def validate(self, data):
        # 验证金额计算逻辑
        if data.get('real_money', 0) < 0:
            raise serializers.ValidationError("优惠后金额不能为负数")

        # 验证优惠金额不超过销货金额
        if data.get('preferential_money', 0) > data.get('all_money', 0):
            raise serializers.ValidationError("优惠金额不能超过销货金额")

        return data


# 销售详情单
class FaKuyouGoodsSalesInfoSerializer(serializers.ModelSerializer):
    purchase_type_display = serializers.CharField(
        source='get_purchase_type_display',
        read_only=True,
        help_text='类型显示值'
    )
    pay_status_display = serializers.CharField(
        source='get_pay_status_display',
        read_only=True,
        help_text='收款状态显示值'
    )
    check_status_display = serializers.CharField(
        source='get_check_status_display',
        read_only=True,
        help_text='对单状态显示值'
    )
    examine_status_display = serializers.CharField(
        source='get_examine_status_display',
        read_only=True,
        help_text='审核状态显示值'
    )

    class Meta:
        model = FaKuyouGoodsSalesInfo
        fields = '__all__'

    def validate(self, data):
        # 验证金额计算逻辑
        if data.get('unit_price', 0) < 0:
            raise serializers.ValidationError("销货单价不能为负数")

        # 验证数量有效性
        if data.get('number', 0) <= 0:
            raise serializers.ValidationError("数量必须大于0")

        return data


# 销售单关联表
class FaKuyouGoodsSaleordSerializer(serializers.ModelSerializer):
    class Meta:
        model = FaKuyouGoodsSaleord
        fields = '__all__'


# 销售单关联详情表
class FaKuyouGoodsSaleordInfoSerializer(serializers.ModelSerializer):
    status_display = serializers.CharField(
        source='get_status_display',
        read_only=True,
        help_text='处理状态显示值'
    )

    class Meta:
        model = FaKuyouGoodsSaleordInfo
        fields = '__all__'

    def validate(self, data):
        # 验证金额计算逻辑
        if data.get('money', 0) > data.get('all_money', 0):
            raise serializers.ValidationError("已收款金额不能超过销货金额")

        # 验证税率范围
        if data.get('tax_rate', 0) < 0 or data.get('tax_rate', 0) > 100:
            raise serializers.ValidationError("税率必须在0-100之间")

        return data


# 销货退货单
class FaKuyouGoodsSalesExchangeSerializer(serializers.ModelSerializer):
    order_type_display = serializers.CharField(
        source='get_order_type_display',
        read_only=True,
        help_text='单据类型显示值'
    )
    purchase_type_display = serializers.CharField(
        source='get_purchase_type_display',
        read_only=True,
        help_text='退货类型显示值'
    )
    pay_status_display = serializers.CharField(
        source='get_pay_status_display',
        read_only=True,
        help_text='收款状态显示值'
    )
    check_status_display = serializers.CharField(
        source='get_check_status_display',
        read_only=True,
        help_text='对单状态显示值'
    )
    examine_status_display = serializers.CharField(
        source='get_examine_status_display',
        read_only=True,
        help_text='审核状态显示值'
    )
    retreat_status_display = serializers.CharField(
        source='get_retreat_status_display',
        read_only=True,
        help_text='退款状态显示值'
    )

    class Meta:
        model = FaKuyouGoodsSalesExchange
        fields = '__all__'

    def validate(self, data):
        # 验证退货数量不超过原始数量
        if data.get('retreat_number', 0) > data.get('number', 0):
            raise serializers.ValidationError("退货数量不能超过原始销售数量")

        # 验证退款金额逻辑
        if data.get('retreat_money', 0) > data.get('all_money', 0):
            raise serializers.ValidationError("退款金额不能超过原始销售金额")

        return data


# 销货退货详情单
class FaKuyouGoodsSalesExchangeInfoSerializer(serializers.ModelSerializer):
    purchase_type_display = serializers.CharField(
        source='get_purchase_type_display',
        read_only=True,
        help_text='销售类型显示值'
    )
    pay_status_display = serializers.CharField(
        source='get_pay_status_display',
        read_only=True,
        help_text='收款状态显示值'
    )
    check_status_display = serializers.CharField(
        source='get_check_status_display',
        read_only=True,
        help_text='对单状态显示值'
    )
    examine_status_display = serializers.CharField(
        source='get_examine_status_display',
        read_only=True,
        help_text='审核状态显示值'
    )

    class Meta:
        model = FaKuyouGoodsSalesExchangeInfo
        fields = '__all__'

    def validate(self, data):
        # 验证退货数量不能超过原始数量
        if data.get('retreat_number', 0) > data.get('number', 0):
            raise serializers.ValidationError("退货数量不能超过原始数量")

        # 验证退款金额不能超过实际金额
        if data.get('retreat_money', 0) > data.get('all_money', 0):
            raise serializers.ValidationError("退款金额不能超过购货金额")

        return data


# 销货退货关联单
class FaKuyouGoodsSaleExcSerializer(serializers.ModelSerializer):
    type_display = serializers.CharField(
        source='get_type_display',
        read_only=True,
        help_text='单据类型显示值'
    )

    class Meta:
        model = FaKuyouGoodsSaleExc
        fields = '__all__'
        extra_kwargs = {
            'exchange_id': {'required': True},
            'purchase_id': {'allow_null': True},
        }

    def validate(self, data):
        # 验证关联ID有效性
        if data.get('exchange_id', 0) <= 0:
            raise serializers.ValidationError("交换单ID必须大于0")

        # 根据类型验证业务规则
        if data.get('type') == '1' and not data.get('purchase_id'):
            raise serializers.ValidationError("销货单关联必须指定采购单ID")

        return data


# 销货退货关联详情单
class FaKuyouGoodsSaleExcInfoSerializer(serializers.ModelSerializer):
    type_display = serializers.CharField(
        source='get_type_display',
        read_only=True,
        help_text='关联类型显示值'
    )
    status_display = serializers.CharField(
        source='get_status_display',
        read_only=True,
        help_text='审核状态显示值'
    )

    class Meta:
        model = FaKuyouGoodsSaleExcInfo
        fields = '__all__'

    def validate(self, data):
        # 验证退款金额不超过购货金额
        if data.get('money', 0) > data.get('all_money', 0):
            raise serializers.ValidationError("退款金额不能超过购货金额")

        # 根据类型验证业务规则
        if data.get('type') == '1' and not data.get('purchase_id'):
            raise serializers.ValidationError("销货订单关联必须指定采购单ID")

        return data
